<?php
	session_start();
	if($_SESSION["username"] == "")
	{
		header("location:login.php");
		exit();
	}
	include 'connectDB.php';
	$strSQL = "SELECT * FROM tbl_user WHERE username = '".$_SESSION["username"]."'  ";
	$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
	$objResult = mysql_fetch_array($objQuery);
?>
<html>
<head>
<title>Welcome to Merchant Manage.</title>
<link rel="stylesheet" type="text/css" href="style.css">
<script src="js/jquery-1.11.1.min.js"></script>
<script src="js/jquery-1.9.1.js"></script>
<script>
$(document).ready(function() {
	$("#panels").hide();
	$("#panels2").hide();
	$( "#flipbtn" ).click(function() {
	$( "#panels	" ).toggle( "slow" );
});
});

function addMer(){
	var merchantname = $("#mername").val();
	var description = $("#des").val();
	var longtitude = $("#long").val();
	var latitude = $("#lati").val();

	$.ajax({
			type: "POST",
	        url: "merchant_service.php",
	        data:{	function:'add',
	        mername:merchantname,
	        desc:description,
	        longti:longtitude,
	        lati:latitude },
	        dataType: "text"
		})
		.done(function(data) {
			//$("#frmListMer").html(data);
			alert("Add Merchant Success !");
			//window.location.assign("http://127.0.0.1/23machine/23machine/merchant_ajax.php")
			$("#listMer").html(data);
			$("#panels").hide();
		});
}

function load(){
	var merchantid = $("#merid").val();
	var merchantname = $("#mername").val();
	var description = $("#des").val();
	var longtitude = $("#long").val();
	var latitude = $("#lati").val();
	$.ajax({
			type: "POST",
	        url: "merchant_service.php",
	        //data: {inputmername:mername,inputdes:description,inputlong:longtitude,inputlati:latitude}
	        data:{'function':'loadList',
	        merid:merchantid,
	        mername:merchantname,
	        desc:description,
	        longti:longtitude,
	        lati:latitude },
	        dataType: "text"
		})
		.done(function(data) {
			//$("#frmListMer").html(data);
			//alert("Add Merchant Success !");
			//window.location.assign("http://127.0.0.1/23machine/23machine/merchant_ajax.php");
			$("#listMer").html(data);
			//alert("data");
			//alert( "Data Loaded: " + data );
		});
}

function delMer(id){
	$("#mer_id").val(id);
	var mer_id  = $("#mer_id").val();

	var answer = confirm ("Are you sure you want to delete from the database?");
	if (answer)
	{
		 $.ajax({
           type:"POST",
           url: "merchant_service.php",
           data: {function:'del',mer_id : mer_id },

           success: function(data){
			 alert(data);
			 load();
          }
         });
    }
}

function editMer(id,name,des,longti,lati){

	$("#mer_id").val(id);
	var mer_id  = $("#mer_id").val();

	var answer = confirm ("Are you sure you want to Edit from the database?");
	if (answer)
	{
	$("#panels2").slideDown("slow");
		$("#editmername").val(name);
		$("#editdes").val(des);
		$("#editlong").val(longti);
		$("#editlati").val(lati);

	}
	
}

function editSave(){

	var mer_id =$("#mer_id").val();
	var mer_name = $("#editmername").val();
	var description = $("#editdes").val();
	var longtitude = $("#editlong").val();
	var latitude = $("#editlati").val();

			$.ajax({
				type: "POST",
				url: "merchant_service.php",
				data: {function:'saveEdit', mer_id: mer_id, mer_name:mer_name,description:description, longtitude:longtitude , latitude:latitude} ,
				success: function(data) {
					//alert(data);
					$("#listMer").html(data);
					$("#panels2").slideUp("slow");
					//load();
				}
			});
}
</script>

</head>

<body onload="load()">
	<input type="hidden" id="mer_id"/>
<!-- start header-->
<div id="header">
	<div class="logo">Merchant Manage</div>
	<div class="user" >Hi !	 <?=$objResult["username"];?><br><a href="logout.php" name="logout" id="logout" />logout</a></div>
</div><!-- end header-->

<!-- start container-->
<div id="container">
	<div id="navigator">
		<a href="dashboard.php">dashboard</a> > <a href="merchant_ajax.php">merchant</a>
	</div>
	<div id="flipbtn"><input type="button" name="addMer" id="add" value="+ add merchant"/></div>
	<br>

	<!--start panel show form add-->
	<div id="panels">
	<form name="frmAddMer" action="merchant_service.php" method="post" id="formAdd">
		<table border="0 " cellpadding="10" cellspacing="0" style="margin-left: 50px;" id="tbadd">
			<!--<tr>
				<td>Merchant ID :</td>
				<td><input type="text" name="txtMerID" id="merid" disabled="" /></td>
			</tr> -->
			<tr>
				<td>Merchant Name :</td>
				<td><input type="text" name="txtMerName" id="mername" /></td>
			</tr>
			<tr>
				<td>Description :</td>
				<td><textarea name="txtDes" cols="30" rows="5" id="des" ></textarea></td>
			</tr>
			<tr>
				<td>Longtitude :</td>
				<td><input type="text" name="txtLong" id="long" /></td>
			</tr>
			<tr>
				<td>Latitude :</td>
				<td><input type="text" name="txtLa" id="lati"/></td>
			</tr>
		</table>
		</form>
	    <input type="button" name="submit" id="submit" value="submit" onClick="addMer()">
	</div><!--end panel show form add-->
	<br>
	<br>
	<!--start panel show form edit-->
	<div id="panels2">
		<p>Edit Merchant.</p>
		<form name="frmEditMer" id="formEdit"  method="post">
		<table border="0 " cellpadding="10" cellspacing="0" style="margin-left: 50px;">

		<!--	<tr>
				<td>Merchant ID :</td>
				<td><input type="text" name="txtMerID" style="padding: 10px;" disabled="" value="<?php echo $objResult[mer_id];?>"></td>
			</tr>-->
			<tr>
				<td>Merchant Name :</td>
				<td><input type="text" name="txtMerName" id="editmername" style="padding: 10px;" ></td>
			</tr>
			<tr>
				<td>Description :</td>
				<td><textarea name="txtDes" id="editdes" cols="30" rows="5" style="padding: 12px;"></textarea></td>
			</tr>
			<tr>
				<td>Longtitude :</td>
				<td><input type="text" name="txtLong" id="editlong" style="padding: 10px;" /></td>
			</tr>
			<tr>
				<td>Latitude :</td>
				<td><input type="text" name="txtLa" id="editlati" style="padding: 10px;" /></td>
			</tr>
			<tr>
				<td></td>
				<td><input type="button" name="submit" id="edit" onclick="editSave()" value="update" style="padding: 10px;"></td>
			</tr>
		</table>
		</form>
	</div><!--end panel show form edit-->



	<!--start table show list merchant-->
    <span id="listMer"></span>


</div>
<!-- end container-->
</body>
</html>
